﻿using System.Collections.Generic;
using QFramework;
using UnityEngine;

namespace HareGame
{
    public class PointManager : MonoBehaviour, ISingleton
    {
        public float Size = 1;
        public Color Color;

        [SerializeField]
        private PointNode[] _nodes = null;
        public PointNode[] Nodes
        {
            get
            {
#if UNITY_EDITOR
                return GetComponentsInChildren<PointNode>();
#else
                if (_nodes == null||_nodes.Length == 0)
                {
                    _nodes = GetComponentsInChildren<PointNode>();
                }
                return _nodes;
#endif
            }
        }

        private Dictionary<string, PointNode> _cache = new Dictionary<string, PointNode>();

        [SerializeField]
        private List<AIComponent> _AIs = new List<AIComponent>();

        /// <summary>
        /// 初始化
        /// </summary>
        public void OnSingletonInit()
        {

        }

        /// <summary>
        /// 定义单例
        /// </summary>
        public static PointManager Instance
        {
            get { return MonoSingletonProperty<PointManager>.Instance; }
        }

        public void AddAI(AIComponent ai)
        {
            if (!_AIs.Contains(ai))
            {
                _AIs.Add(ai);
            }
        }

        private void LateUpdate()
        {
            foreach (var v in _AIs)
            {
                if (v != null && v.gameObject.activeInHierarchy)
                    v.OnMoveUpdate();
            }
        }

        public PointNode FindNode(string name)
        {
            if (_cache.ContainsKey(name))
                return _cache[name];

            foreach (var v in Nodes)
            {
                if (v.name == name)
                {
                    _cache.Add(name, v);
                    return v;
                }
            }
            LogKit.E("找不到名为:<color=red>" + name + "</color>的路径节点");
            return null;
        }
    }

    public enum PathRange
    {
        All = -1,
        外围 = 0,
        室内,
    }
}